package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ten_fei
 * @create 2021-02-18 11:09
 */

/**
 * Java线程池的使用
 */

class NumberThread1 implements Runnable{
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (i%2==0){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
}

class NumberThread2 implements Runnable{
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (i%2!=0){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }
        }
    }
}




public class ThreadPool {
    public static void main(String[] args) {
        //1.提供指定线程数量的线程池
        ExecutorService service= Executors.newFixedThreadPool(10);
        //2.执行指定的线程操作。需要提供实现Runable接口或Callable接口实现类的对象
        service.execute(new NumberThread1());  //适用于Runable
        service.execute(new NumberThread2());  //适用于Runable
//        service.submit();   //适用于Callable
        //完成后关闭线程池
        service.shutdown();
    }
}
